package com.jiamao.goplay.ui.main.fragment;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.view.AppBarStateChangeListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnMultiListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by goldze on 2018/7/18.
 */

public class TabBar1Fragment extends BaseFragment {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_two_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    private int mScrollY = 0;
    private int mOffset = 0;

    private int mZoomViewWidth ;
    private int mZoomViewHeight ;
    int off  = 0;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initData() {
        super.initData();

        ImmersionBar.setTitleBar(this, binding.getRoot().findViewById(R.id.toolbar));

       AppBarLayout appBarLayout = (AppBarLayout)  binding.getRoot().findViewById(R.id.app_bar);
        Toolbar linearLayout = binding.getRoot().findViewById(R.id.toolbar);


        ImageView imageView = binding.getRoot().findViewById(R.id.img);


//        CustomCoordinatorLayout customCoordinatorLayout = binding.getRoot().findViewById(R.id.curr);
       // customCoordinatorLayout.setmZoomView(imageView);
     //   customCoordinatorLayout.setmMoveView(binding.getRoot().findViewById(R.id.toolbar),binding.getRoot().findViewById(R.id.refresh));
        Button button = binding.getRoot().findViewById(R.id.btntext);
        SmartRefreshLayout swipeRefreshLayout = (SmartRefreshLayout)  binding.getRoot().findViewById(R.id.refresh);

        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipeRefreshLayout.finishRefresh();
                        }
                    },1000)     ;
            }
        });
        imageView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){

                    @Override
                    public void onGlobalLayout(){
                        //只需要获取一次高度，获取后移除监听器
                        imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        //这里高度应该定义为成员变量，定义为局部为展示代码方便

                        mZoomViewWidth = imageView.getMeasuredWidth();
                        mZoomViewHeight = imageView.getMeasuredHeight();
                    }

                });
        swipeRefreshLayout.setDragRate(1f);
        swipeRefreshLayout.setHeaderMaxDragRate(1);//最大显示下拉高度/Header标准高度
        swipeRefreshLayout.setHeaderTriggerRate(0.4f);//触发刷新距离 与 HeaderHeight 的比率1.0.4

//        swipeRefreshLayout.setOnMultiListener(new new OnMultiListener() {
//            @Override
//            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
//
//                Log.d("HTML","percent:"+percent+" OFFSET:"+offset+" headHe"+headerHeight+" maxL"+maxDragHeight);
//                // 下拉后的操作，这里只是做了平移效果`setTranslationY`,当然你可以做很多酷炫的效果
//
//                int distance = (int) ((offset - off) * 0.6); // 滚动距离乘以一个系数
//                off = offset;
//              //  imageView.setTranslationY(offset / 2);
////                imageView.requestLayout();
//                //设置图片宽度变化   当达到指定设置的指定值后  宽度停止  只向下移动
////                lp.width = (int) (mZoomViewWidth + zoom);
////                lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + zoom) / mZoomViewWidth));
////                ((MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
//
//
//                ViewGroup.LayoutParams lp = imageView.getLayoutParams();
//                lp.width = (int) (mZoomViewWidth * ((mZoomViewWidth + distance) / mZoomViewWidth));
//                lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + distance) / mZoomViewWidth));
//                ((ViewGroup.MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
//                imageView.setLayoutParams(lp);
//
//
////                ViewGroup.LayoutParams lp = imageView.getLayoutParams();
////                lp.width = (int) mZoomViewWidth +offset;
////                lp.height = mZoomViewHeight+offset;
////           //     ((ViewGroup.MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
////                imageView.setLayoutParams(lp);
////                imageView.requestLayout();
//
//                if (offset <= 100) {
////                    ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
////                    layoutParams.height +=  f;
////
////                    imageView.requestLayout();
//                }
//               // toolbar.setAlpha(1 - Math.min(percent, 1));
//
//            }
//
//            @Override
//            public void onHeaderReleased(RefreshHeader header, int headerHeight, int maxDragHeight) {
//
//            }
//
//            @Override
//            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int maxDragHeight) {
//
//            }
//
//            @Override
//            public void onHeaderFinish(RefreshHeader header, boolean success) {
////                ViewGroup.LayoutParams lp = imageView.getLayoutParams();
////                //    lp.width = (int) (mZoomViewWidth * ((mZoomViewWidth + offset) / mZoomViewWidth));
////                lp.height = mZoomViewHeight;
////                //     ((ViewGroup.MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
////                imageView.setLayoutParams(lp);
////                imageView.requestLayout();
//            }
//
//            @Override
//            public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {
//
//            }
//
//            @Override
//            public void onFooterReleased(RefreshFooter footer, int footerHeight, int maxDragHeight) {
//
//            }
//
//            @Override
//            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int maxDragHeight) {
//
//            }
//
//            @Override
//            public void onFooterFinish(RefreshFooter footer, boolean success) {
//
//            }
//
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//
//            }
//
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//
//            }
//
//            @Override
//            public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
//
//            }
//        });
//        复制代码
        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)  binding.getRoot().findViewById(R.id.toolbar_layout);
        NestedScrollView scrollView =  binding.getRoot().findViewById(R.id.item_detail_container);


        int color = ContextCompat.getColor(getContext(), R.color.orange);
       Log.d("HTML","AA "+appBarLayout.getHeight());
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {

                    linearLayout.setBackgroundColor(0);
                    //展开状态
                    button.setText("白色");
                    ImmersionBar.with(TabBar1Fragment.this).statusBarDarkFont(false);
                }else if(state == State.COLLAPSED){
                    linearLayout.setBackgroundColor(Color.WHITE);
                    button.setText("黑色");
                    ImmersionBar.with(TabBar1Fragment.this).statusBarDarkFont(true);

                }else {
                    //中间状态
                   // linearLayout.setBackgroundColor(color);
                 //   linearLayout.setAlpha(150);
                 //   toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);

                }
            }
        });

//        collapsingToolbarLayout.post(() -> {
//
//
//

//
//        //距离最小值
//        float minDistance = ConvertUtils.dp2px(85);
//        //距离范围差值
//        float deltaDistance = ConvertUtils.dp2px( 300) -minDistance ;
////            int offHeight = collapsingToolbarLayout.getHeight() - ImmersionBar.getStatusBarHeight(this);

            appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {





              //  ImmersionBar.with(this).statusBarDarkFont(Math.abs(i) >= offHeight, 0.2f).init();

        });


        //ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.toolbar));
    }
}
